package com.sumer.psychology.blog.application.command;

import com.sumer.psychology.blog.application.command.BlogApplicationService;
import com.sumer.psychology.blog.application.command.CreateBlogCommand;
import com.sumer.psychology.blog.application.command.DeleteCategoryCommand;
import com.sumer.psychology.blog.domain.model.*;
import com.sumer.psychology.common.domain.model.Author;
import com.sumer.psychology.common.domain.model.CommonDomainRegistry;
import com.sumer.psychology.common.domain.model.UserService;
import com.sumer.psychology.common.port.adapter.service.MockUserInRoleAdapter;
import com.sumer.psychology.common.spring.SpringHibernateSessionProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import javax.inject.Inject;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

/**
 * User: zhenguang.zhu
 * Date: 2014/9/4
 * Time: 18:18
 */
@ContextConfiguration({
        "/applicationContext-blog-test.xml",
        "/applicationContext-common-test.xml",
        "/applicationContext-common-orm.xml"})
public class BlogApplicationServiceTestWithDB extends AbstractTransactionalJUnit4SpringContextTests {
    @Inject
    private BlogApplicationService blogApplicationService;

    @Inject
    private BlogRepository blogRepository;

    @Inject
    private SpringHibernateSessionProvider sessionProvider;

    @Before
    public void setUp() throws Exception {
        executeSqlScript("/data/schema.sql", false);
        executeSqlScript("/data/data.sql", false);
    }

    @After
    public void tearDown() throws Exception {
        sessionProvider.session().clear();
    }

    /**
     * 测试一个category和blog关联的操作
     */
    @Test
    public void should_write_a_blog_with_category() {
        CreateBlogCommand createBlogCommand = new CreateBlogCommand(
                new Author("1", "zhuzhenguang"),
                "a blog title",
                "a blog content",
                "b2ce4537-185d-48a0-914b-f55bae7b6892");

        blogApplicationService.newBlog(createBlogCommand);
    }

    @Test
    public void should_remove_category_of_blogs() {
        Blog beforeBlog = blogRepository.blogOfId(new BlogId("0599c9fb-b92a-41d1-b7cd-6241881a2316"));
        assertThat(beforeBlog.category(), is(new Category(
                new CategoryId("b2ce4537-185d-48a0-914b-f55bae7b6892"),
                new Author("1"),
                "default name")));

        blogApplicationService.removeCategoryOfBlogs(new DeleteCategoryCommand("b2ce4537-185d-48a0-914b-f55bae7b6892"));

        Blog afterBlog = blogRepository.blogOfId(new BlogId("0599c9fb-b92a-41d1-b7cd-6241881a2316"));
        assertThat(afterBlog.category(), nullValue());
    }

}
